package pers.huangyuhui.sms.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBatisUtil {

    //    public static void main(String[] args) throws IOException {
//        SqlSession sqlSession=getSqlSession();
//        AdminMapper adminMapper=sqlSession.getMapper(AdminMapper.class);
//
//        System.out.println(adminMapper.getUserInfo("admin","1"));
//        System.out.println(adminMapper.modifyPassword("970719","001"));
////        sqlSession.commit();
//    }
    private static SqlSessionFactory sqlSessionFactory;
    static {
        String resources = "myBatis-config.xml";
        //创建流
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //初始化mybatis,创建SqlSessionFactory类的对象
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }
    public static SqlSession getSqlSession(){
        return  sqlSessionFactory.openSession(true);
    }

    public static  <T> T getMapper(Class<T> type){
        SqlSession sqlSession=getSqlSession();
        return  sqlSession.getMapper(type);
    }

}